using System;
using System.Collections.Generic;

namespace AmazonWebService
{
	class MainClass
	{
		private static void DisplayUsage () {
			Console.WriteLine("USAGE:  " + System.Environment.GetCommandLineArgs()[0] + " <OPTION> \"<Search Strings>\" [<Search Result Page No.>]");
			Console.WriteLine("OPTION: -a or --author - Conducts search by Author.");
			Console.WriteLine("        -t or --title  - Conducts search by Title.");
			Console.WriteLine("        -i or --isbn   - Conducts search by ISBN.");
		}
		
		public static void Main (string[] args)
		{
			List<AmazonWSBook> books = null;
			AmazonWSBook book;
			int searchResultPageNo;
			
			if ((args.Length < 2) || (args.Length > 3)) {
				DisplayUsage();
				System.Environment.Exit(-1);
			}
			if (args.Length == 3) {
				searchResultPageNo = Convert.ToInt32(args[2]);
			}else {
				searchResultPageNo = 1;
			}
			try {
				switch (args[0]) {
				case "--author":
				case "-a":
					Console.WriteLine("Searching for Author having \"" + args[1] + "\" using Amazon Web Service...\n");
					books = AmazonWSBook.SearchBooksByAuthor(args[1], searchResultPageNo);
					break;
				case "--title":
				case "-t":
					Console.WriteLine("Searching for Title having \"" + args[1] + "\" using Amazon Web Service...\n");
					books = AmazonWSBook.SearchBooksByTitle(args[1], searchResultPageNo);
					break;
				case "--isbn":
				case "-i":
					Console.WriteLine("Searching for ISBN having \"" + args[1] + "\" using Amazon Web Service...\n");
					books = new List<AmazonWSBook>(0);
					books.Add(AmazonWSBook.GetBookByISBN(args[1]));
					break;
				default:
					Console.WriteLine("Unknown OPTION: '" + args[0] + "'.");
					DisplayUsage();
					System.Environment.Exit(-1);
					break;
				}
			}
			catch (Exception ex) {
				if (ex.GetType().Equals(typeof(System.ServiceModel.CommunicationException))) {
					Console.WriteLine("Encountered System.ServiceModel.CommunicationException!");
					Console.WriteLine("Search Result Page too long, please try a different search string(s).");
					System.Environment.Exit(-1);
				}else if (ex.GetType().Equals(typeof(System.ServiceModel.EndpointNotFoundException))) {
					Console.WriteLine("Encountered System.ServiceModel.EndpointNotFoundException!");
					Console.WriteLine("Couldn't connect to Amazon, please check your internet connection.");
					System.Environment.Exit(-1);
				}else if (ex.GetType().Equals(typeof(System.NullReferenceException))) {
					Console.WriteLine("Encountered System.NullReferenceException!");
					Console.WriteLine("Search Result Page returned empty, maximum Search Result Page No. exceeded.");
					System.Environment.Exit(-1);
				}else {
					throw ex;
				}
			}
			for (int i = 0; i < books.Count; i++) {
				book = books[i];
				Console.WriteLine("Book #" + (((searchResultPageNo - 1) * 10) + i));
				Console.WriteLine("Book ISBN: " + book.ISBN);
				Console.WriteLine("Book Title: " + book.Title);
				// Check if Author returned is not NULL; Which occurs when NO Author was returned.
				if (book.Author != null) {
					Console.WriteLine("Book Author: " + book.Author[0]);
				}else {
					Console.WriteLine("Book Author:");
				}
				Console.WriteLine("Book Publisher: " + book.Publisher);
				Console.WriteLine("Book Genre: " + book.Genre);
				Console.WriteLine("Book Synopsis: " + book.ShortSynopsis);
				// Check if Image returned is not null; Which occurs whenever ItemSearchRequest.ResponseGroup was set to "Small".
				if (book.LargeImage != null) {
					Console.WriteLine("Book Image URL: " + book.LargeImage.URL);
				}else {
					Console.WriteLine("Book Image URL:");
				}
				Console.WriteLine();
			}
		}
	}
}

